var vm = new Vue({
	el: "#app",
	data: {
		totalMoney: 0,
		prodcutList: [],
		checkedFlag: false,
		delFlag: false,
		curProduct: ''
	},
	mounted: function() {
		this.cartView();
	},
	filters: {
		formatMoney: function(value) {
			return "￥" + value.toFixed(2);
		}
	},
	methods: {
		cartView: function() {
			var _this = this;
			this.$http.get("data/cartData.json", {
				id: 123
			}).then(function(res) {
				_this.prodcutList = res.data.result.list;
//				_this.totalMoney = res.data.result.totalMoney;
			})
		},
		changeCount: function(o, num) {
			o.productQuantity += num;
			if(o.productQuantity < 1)
				o.productQuantity = 1;
			this.calcTotalPrice();
		},
		selectPro: function(o) {
			if(typeof(o.checked) == "undefined") {
				this.$set(o, "checked", true);
			} else {
				o.checked = !o.checked;
			}
			this.calcTotalPrice();
		},
		checkall: function(a) {
			this.checkedFlag = a;
			this.prodcutList.forEach((o, index) => {
				if(typeof(o.checked) == "undefined") {
					this.$set(o, "checked", true);
				} else {
					o.checked = this.checkedFlag;
				}
			});
			this.calcTotalPrice();
		},
		calcTotalPrice: function() {
			var _this = this;
			this.totalMoney = 0;
			this.prodcutList.forEach(function(item, index) {
				if(item.checked) {
					_this.totalMoney += item.productPrice * item.productQuantity;
				}
			});
		},
		delConfirm: function(item) {
			this.delFlag = true;
			this.curProduct = item;
		},
		delProduct: function() {
			var index = this.prodcutList.indexOf(this.curProduct);
			this.prodcutList.splice(index, 1);
			this.delFlag = false;
			this.calcTotalPrice();
		}
	}
});
Vue.filter("money", function(value, type) {
	return "￥" + value.toFixed(2) + type;
})

function jian(o) {
	console.log(o);
}


